/*
 * NFC_reader.c
 *
 * Created: 20.02.2013 14:31:43
 *  Author: saenko
 */ 


#include <avr/io.h>
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/delay.h>

#include "globals.h"

#define F_CPU 8000000UL

#include "uart.h"
#include "SPI_M.h"
#include "HEXtoASCII.h"
#include "trf7960.h"

#include "hardware.h"

#define IDLE 0
#define OUT_REG_SUM 1
#define OUT_IRQ 2
#define ON_CHIP 3
#define OFF_CHIP 4
#define LOAD_FIFO 5
#define OUT_FIFO_SET 6
#define SET_TX 7
#define SEND_PACK 8
#define FIND_TAG 9

#define TRFDisable cbi(PORTD,PC5);
#define TRFEnable sbi(PORTD,PC5);

unsigned char state = IDLE;

unsigned char default_reg[] = {0x01,0x02,0x00,0x00,0xC2,0x00,0x00,0x0E,0x1F,0x11,0x40,0x87};

int main(void)
{	
	
	sbi(DDRD,PD5);
	uart_init();
	spi_init();
	trf7960_init();
	sei();
	
	printstr_p(PSTR("ATMega88 is ready\n"));	
	
    while(1)
    {
		//spi_send_pack();
	//	spi_read_reg(0x04);
        if(uart_flags.rxstring_end)
		{
			uart_flags.rxstring_end = 0;
			switch(rxstring[0])
			{
			case 'A': state = OUT_REG_SUM; break;
			case 'I': state = OUT_IRQ; break;
			case 'O': state = ON_CHIP; break;
			case 'o': state = OFF_CHIP; break;
			case 'Q': state = LOAD_FIFO; break;
			case 'F': state = OUT_FIFO_SET; break;
			case 'T': state = SET_TX; break;
			case 'X': state = SEND_PACK; break;
			case '!': state = FIND_TAG; break;
			}
	
		}
		
		switch(state)
		{
		case OUT_REG_SUM:
					state = IDLE;
					putchr(13);
					for(unsigned char i=0;i<0x0C;i++)
					{
						hex_to_ascii(i);
						putchr(ascii_hi);
						putchr(ascii_low);
						
						
						putchr(':');
						hex_to_ascii(spi_read_reg(0x40+i));
						putchr(ascii_hi);
						putchr(ascii_low);
						
						putchr(':');
						hex_to_ascii(default_reg[i]);
						putchr(ascii_hi);
						putchr(ascii_low);	
						
										
											
						putchr(13);
					}
		break;	
		case OUT_IRQ:
					state = IDLE;
					putchr(13);
	
					hex_to_ascii(0x6C);
					putchr(ascii_hi);
					putchr(ascii_low);
							
					putchr(':');
	
					hex_to_ascii(spi_read_IRQ_status());
					putchr(ascii_hi);
					putchr(ascii_low);
					
					
					putchr(13);
					
		break;
		case ON_CHIP:
					state = IDLE;
					
					spi_write_reg(0x00,0b00100001);
		break;
		
		case OFF_CHIP:
					state = IDLE;
					spi_write_reg(0x00,0b00000001);
		break;
		
		case LOAD_FIFO:
					state = IDLE;
					load_to_fifo(0x01);
		break;
		
		case OUT_FIFO_SET:
					state = IDLE;
					read_fifo_set();
		break;
		
		case SET_TX:
					state = IDLE;
					cmd_trans_with_crc();
		break;
		
		case SEND_PACK:
					state = IDLE;
					spi_send_pack();
		break;
		
		case FIND_TAG:
					state = IDLE;
		//-------------------------------------------------
					TRFDisable;
					_delay_ms(1);
					TRFEnable;
					_delay_ms(1);

				    InitialSettings();                  // Set MCU Clock Frequency to 13.56 MHz and OOK Modulation
					

					_delay_ms(10);
					
					ENABLE = 1;

					POLLING = 1;
				
					FindTags(0x00);
			

		
		
		//-------------------------------------------------
		break;
		}
	
    }
}

//----------------------------

